package com.yycx.module.bus.provider.handler.supplierbond;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.interceptor.GetInterceptor;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.file.provider.service.SysFileService;
import com.yycx.module.system.client.entity.SysBank;
import com.yycx.module.system.provider.service.SysBankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 垫付款详情信息
 *
 * @author zyf
 */
@Component("supplierBondPaymentGetHandler")
public class SupplierBondPaymentGetHandler implements GetInterceptor {

    @Autowired
    private BaseUserService userService;

    @Autowired
    private SysBankService bankService;
    @Autowired
    private SysFileService fileService;

    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {
        Long orderId = entityMap.getLong("orderId");
        cq.select(BusPayment.class, "paymentNo", "amount", "payDate", "payUserId", "remark", "payBankId","paymentId");
        cq.select(BusSupplier.class, "supplierName");
        cq.select(BusCustom.class, "customName");
        cq.select(BusSupplierBank.class, "bankName", "bankNo", "linkMan");
        cq.eq(true, "orderId", orderId);
        cq.eq(true, "amountType", AmountTypeEnum.SupplierBond);
        cq.createJoin(BusSupplierBank.class).setMainField("bankId");
        cq.createJoin(BusSupplier.class);
        cq.createJoin(BusCustom.class);
    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        Long payUserId = map.getLong("payUserId");
        Long payBankId = map.getLong("payBankId");
        if (FlymeUtils.isNotEmpty(payBankId)) {
            SysBank bank = bankService.getById(payBankId);
            if(FlymeUtils.isNotEmpty(bank)) {
                map.put("payBankName", bank.getBankName());
            }
        }
        if (FlymeUtils.isNotEmpty(payUserId)) {
            BaseUser payUser = userService.getById(payUserId);
            if(FlymeUtils.isNotEmpty(payUser)) {
                map.put("payUserName", payUser.getNickName());
            }
        }
    }
}
